package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_UseArrowSkill;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.CheckUtil;
import com.lineage.server.world.World;
import java.util.Calendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_AttackBow extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_AttackBow.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      pc.setFoeSlayer(false);
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport() && 
        !pc.isPrivateShop() && 
        !pc.isFreezeAtion())
        if (pc.getInventory().getWeight240() >= 197) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(110));
        } else {
          int result = pc.speed_Attack().checkInterval(
              AcceleratorChecker.ACT_TYPE.ATTACK);
          if (result == 2)
            _log.error("要求角色攻击:速度异常(" + pc.getName() + ")"); 
          if (pc.isInvisble())
            return; 
          if (pc.isInvisDelay())
            return; 
          if (pc.isParalyzedX())
            return; 
          if (pc.get_weaknss() != 0) {
            long h_time = Calendar.getInstance().getTimeInMillis() / 1000L;
            if (h_time - pc.get_weaknss_t() > 16L)
              pc.set_weaknss(0, 0L); 
          } 
          int targetId = 0;
          int locx = 0;
          int locy = 0;
          int targetX = 0;
          int targetY = 0;
          try {
            targetId = readD();
            locx = readH();
            locy = readH();
          } catch (Exception e) {
            over();
            return;
          } 
          if (locx == 0)
            return; 
          if (locy == 0)
            return; 
          targetX = locx;
          targetY = locy;
          L1Object target = World.get().findObject(targetId);
          if (target instanceof L1Character && target.getMapId() != pc.getMapId())
            return; 
          CheckUtil.isUserMap(pc);
          if (target instanceof L1NpcInstance) {
            int hiddenStatus = ((L1NpcInstance)target).getHiddenStatus();
            if (hiddenStatus == 1)
              return; 
            if (hiddenStatus == 2)
              return; 
          } 
          if (pc.hasSkillEffect(78)) {
            pc.killSkillEffectTimer(78);
            pc.startHpRegeneration();
            pc.startMpRegeneration();
          } 
          pc.killSkillEffectTimer(32);
          pc.delInvis();
          pc.setRegenState(1);
          if (target != null && !((L1Character)target).isDead()) {
            if (target instanceof L1PcInstance) {
              L1PcInstance tg = (L1PcInstance)target;
              pc.setNowTarget((L1Character)tg);
            } 
            target.onAction(pc);
            return;
          } 
          pc.setHeading(pc.targetDirection(locx, locy));
          L1ItemInstance weapon = pc.getWeapon();
          if (weapon != null) {
            L1ItemInstance arrow, sting;
            int weaponType = weapon.getItem().getType1();
            switch (weaponType) {
              case 20:
                arrow = pc.getInventory().getArrow();
                if (arrow != null) {
                  int arrowGfxid = 66;
                  switch (pc.getTempCharGfx()) {
                    case 8842:
                    case 8900:
                      arrowGfxid = 8904;
                      break;
                    case 8845:
                    case 8913:
                      arrowGfxid = 8916;
                      break;
                    case 7959:
                    case 7967:
                    case 7968:
                    case 7969:
                    case 7970:
                      arrowGfxid = 7972;
                      break;
                    case 13631:
                      arrowGfxid = 13656;
                      break;
                    case 13635:
                      arrowGfxid = 13658;
                      break;
                  } 
                  if (pc.getTempCharGfx() >= 13715 && pc.getTempCharGfx() <= 13745)
                    arrowGfxid = 11762; 
                  arrowAction(pc, arrow, arrowGfxid, targetX, targetY);
                  break;
                } 
                if (weapon.getName().equals("$1821")) {
                  arrowAction(pc, (L1ItemInstance)null, 2349, targetX, targetY);
                  break;
                } 
                nullAction(pc);
                break;
              case 62:
                sting = pc.getInventory().getSting();
                if (sting != null) {
                  int stingGfxid = 2989;
                  switch (pc.getTempCharGfx()) {
                    case 8842:
                    case 8900:
                      stingGfxid = 8904;
                      break;
                    case 8845:
                    case 8913:
                      stingGfxid = 8916;
                      break;
                    case 7959:
                    case 7967:
                    case 7968:
                    case 7969:
                    case 7970:
                      stingGfxid = 7972;
                      break;
                    case 13631:
                      stingGfxid = 13656;
                      break;
                    case 13635:
                      stingGfxid = 13658;
                      break;
                  } 
                  if (pc.getTempCharGfx() >= 13715 && pc.getTempCharGfx() <= 13745)
                    stingGfxid = 11762; 
                  arrowAction(pc, sting, stingGfxid, targetX, targetY);
                  break;
                } 
                nullAction(pc);
                break;
            } 
            return;
          } 
          return;
        }  
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  private void arrowAction(L1PcInstance pc, L1ItemInstance item, int gfx, int targetX, int targetY) {
    pc.sendPacketsAll((ServerBasePacket)new S_UseArrowSkill((L1Character)pc, gfx, targetX, targetY, 1));
    if (item != null)
      pc.getInventory().removeItem(item, 1L); 
  }
  
  private void nullAction(L1PcInstance pc) {
    pc.sendPacketsAll((ServerBasePacket)new S_UseArrowSkill((L1Character)pc));
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
